




# 安装pythong
# pip install pyautogui opencv-python numpy pygetwindow Pillow
import pyautogui
import cv2
import time
import pyautogui
import numpy as np
import pygetwindow as gw
from PIL import Image
from win32gui import GetForegroundWindow, GetWindowRect

# 等待游戏加载完毕
time.sleep(3)

# 单次点击按键
def push_button(acction):
    pyautogui.keyDown(acction)
    pyautogui.keyUp(acction)
    time.sleep(0.3)

# 连续点击按键
def continuous_push_button(acction, count):
    while count > 0:
        push_button(acction)
        count -= 1

# 截图并查找图像模板
def case_png(img_path, img_path2=None, threshold=0.8):
    #  
    screenshot = pyautogui.screenshot()
    screenshot.save('screenshot.png')  # 保存截图到本地
    # 使用Pillow打开截图
    image = Image.open('screenshot.png')
    # 将Pillow图像转换为OpenCV格式
    cv_image = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
    # 加载要查找的图像模板
    template = cv2.imread(img_path)
    # 使用模板匹配方法查找图像位置
    result = cv2.matchTemplate(cv_image, template, cv2.TM_CCOEFF_NORMED)
    # threshold = 0.8  # 设置匹配阈值

    loc = np.where(result >= threshold)
    if len(loc[0]) == 0:
        # 未找到图像模板
        if img_path2 is not None:
            # 尝试查找第二个图像模板
            template2 = cv2.imread(img_path2)
            result2 = cv2.matchTemplate(cv_image, template2, cv2.TM_CCOEFF_NORMED)
            # threshold2 = 0.8  # 设置匹配阈值
            loc2 = np.where(result2 >= threshold)
            if len(loc2[0]) == 0:
                # 未找到第二个图像模板
                return False, None
            else:
                # 取第一个匹配的位置
                pt = (loc2[1][0], loc2[0][0])
                # 计算图像中心的位置
                center = (pt[0] + template2.shape[1] // 2, pt[1] + template2.shape[0] // 2)
                return True, center
        else:
            # 未找到第二个图像模板
            return False, None
    else:
        # 取第一个匹配的位置
        pt = (loc[1][0], loc[0][0])
        # 计算图像中心的位置
        center = (pt[0] + template.shape[1] // 2, pt[1] + template.shape[0] // 2)
        return True, center



# 校验当前打开的窗口
while True:
    # 获取所有窗口
    windows = gw.getAllWindows()
    find_window = False
    # 找到当前活动窗口
    for window in windows:
        if window.isActive:
            print(f"当前活动窗口的标题是: {window.title}")
            # guigubahuang
            if window.title == 'guigubahuang':
                print(f"请将游戏分别率设置为1280x720，并以窗口模式运行")
                find_window = True
                break
    # 找到活动窗口
    if find_window == True:
        break
    else:
        print(f"请将【鬼谷八荒】窗口置于最前")
        time.sleep(1)

# if True:
if case_png('png/leisheng_fight_end.png')[0] == True:
    print(f"战斗结束，点击空格继续")
    push_button('space')
    while case_png('png/leisheng_fight_end_after.png')[0] == False:
        time.sleep(0.5)
    time.sleep(1)
    print(f"点击重新加载")
    push_button('esc')
    # 获取当前活动窗口的句柄
    hwnd = GetForegroundWindow()
    # 获取窗口的位置和大小
    left, top, right, bottom = GetWindowRect(hwnd)
    # 计算窗口的宽度和高度
    width = right - left
    height = bottom - top
    # 定义相对于窗口的相对位置（例如，窗口中心）
    relative_x = width * 0.5
    relative_y = height * 0.5
    # 计算绝对坐标
    absolute_x = left + relative_x
    absolute_y = top + relative_y
    # 移动鼠标到计算出的绝对坐标
    pyautogui.moveTo(absolute_x, absolute_y)
    # 点击鼠标左键
    pyautogui.click()
    # 点击【是】按钮
    # 定义相对于窗口的相对位置（例如，窗口中心）
    relative_x = width * 0.48
    relative_y = height * 0.5
    # 计算绝对坐标
    absolute_x = left + relative_x
    absolute_y = top + relative_y
    # 移动鼠标到计算出的绝对坐标
    pyautogui.moveTo(absolute_x, absolute_y)
    # 点击鼠标左键
    pyautogui.click()

while case_png('png/leisheng_start1.png')[0] == False:
    time.sleep(0.5)

# if case_png('png/menu.png')[0] == True:
#     print(f"菜单被打开，关闭菜单")
#     push_button('esc')

if case_png('png/leisheng_start1.png')[0] == True:
    push_button('A')
    time.sleep(0.5)

# elif case_png('png/leisheng_start.png')[0] == True:
    # pyautogui.moveTo(100, 150)
    # pyautogui.moveTo(100, 200)

    # 获取当前活动窗口的句柄
    hwnd = GetForegroundWindow()

    # 获取窗口的位置和大小
    left, top, right, bottom = GetWindowRect(hwnd)

    # 计算窗口的宽度和高度
    width = right - left
    height = bottom - top

    # 定义相对于窗口的相对位置（例如，窗口中心）
    relative_x = width * 0.5
    relative_y = height * 0.5

    # 计算绝对坐标
    absolute_x = left + relative_x
    absolute_y = top + relative_y

    # 移动鼠标到计算出的绝对坐标
    pyautogui.moveTo(absolute_x, absolute_y)

    # 点击鼠标左键
    pyautogui.click()

    # 等待1秒
    time.sleep(1) 

    if case_png('png/leisheng_start_ok.png')[0] == True:

        # 计算[开始战斗]按钮的位置
        relative_x = width * 0.5
        relative_y = height * 0.75
        # 计算绝对坐标
        absolute_x = left + relative_x
        absolute_y = top + relative_y
        # 移动鼠标到计算出的绝对坐标
        pyautogui.moveTo(absolute_x, absolute_y)
        # 点击鼠标左键
        pyautogui.click()
        time.sleep(1)

        # 点击[开始战斗]按钮
        push_button('space')

        while case_png('png/leisheng_fight.png')[0] == False:
            if case_png('png/err_fight.png')[0] == True:
                print(f"错误遇敌，点击重新加载")
            time.sleep(0.5)

        # 点击空格[开始战斗]
        push_button('space')
        time.sleep(0.5)

        # 点击空格使用身法
        push_button('space')
        # 点击鼠标右键使用绝技
        pyautogui.click(button='right')
        # 召唤人物
        push_button('F')

        while True:
            if case_png('png/leisheng_fight_end.png')[0] == True:
                print(f"战斗结束")
                break

            # 鼠标持续跟踪雷神的位置
            found, center = case_png('png/leisheng.png', 'png/leisheng1.png', 0.5)
            if found:
                if case_png('png/jueji_ok.png')[0] == True:
                    pyautogui.click(button='right')
                if case_png('png/kongge_ok.png')[0] == True:
                    push_button('space')

                # 移动鼠标到雷神的位置
                print(f"找到雷神, 位置是: {center}")
                pyautogui.moveTo(center[0], center[1])
                pyautogui.mouseDown()
                time.sleep(1)
                pyautogui.mouseUp()
            else:
                # 鼠标移动到当前窗口的屏幕中心
                print(f"未找到雷神, 移动鼠标到屏幕中心")
                relative_x = width * 0.5
                relative_y = height * 0.5
                # 计算绝对坐标
                absolute_x = left + relative_x
                absolute_y = top + relative_y
                # 移动鼠标到计算出的绝对坐标
                pyautogui.moveTo(absolute_x, absolute_y)
                pyautogui.mouseDown()
                time.sleep(0.5)
                pyautogui.mouseUp()
        # 等待1秒
        time.sleep(1)

        # 检查boss掉落物
        relative_x = width * 0.5
        relative_y = height * 0.75
        # 计算绝对坐标
        absolute_x = left + relative_x
        absolute_y = top + relative_y
        # 移动鼠标到计算出的绝对坐标
        pyautogui.moveTo(absolute_x, absolute_y)
        
else:
    print("未找到初始状态: png/leisheng_start1.png")